package com.lhy.controller;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.lhy.domain.ApplyMsg;
import com.lhy.domain.Friend;
import com.lhy.domain.Page;
import com.lhy.domain.Result;
import com.lhy.domain.Student;
import com.lhy.service.IUserService;

@Controller
@RequestMapping("/applymsghandle")
public class ApplyMsgHanleController {
	@Autowired
	private IUserService userService;
	

	@RequestMapping("")
	public ModelAndView list(HttpSession s,String pageNow){
		Integer pagenow = 1;
		if(!StringUtils.isEmpty(pageNow)){
			pagenow = Integer.valueOf(pageNow);
		}
		ModelAndView mv = new ModelAndView();
		try {
			Integer sequence = Integer.valueOf(s.getAttribute("usersequence").toString());
			Integer count = userService.selectCountByApplyMsg(sequence);
			Page page = new Page(count, pagenow);
			Integer startIndex = pagenow*page.getPageSize()-7;
			Integer endIndex = pagenow*page.getPageSize();
			List<ApplyMsg> list = new ArrayList<>();
			if(count > 0){
				list = userService.selectApplyMsgByUsersequenceAndPage(sequence,startIndex,endIndex);
			}

			mv.addObject("list",list);
			mv.addObject("page",page);
			mv.setViewName("applymsghandle");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mv;
	}
	
	@RequestMapping("/agree")
	public @ResponseBody
	Map<String, Object> agree(String usersequence, String msgsequence, String remark){
		Map<String, Object> returnMap = new HashMap<>();
		Result result=new Result();
		result.setCode("1");
		Date date = new Date();       
		Timestamp create_dt = new Timestamp(date.getTime());
		try{
			ApplyMsg applymsg =new ApplyMsg();
			Student u = userService.queryStudentBySequence(Integer.valueOf(usersequence));
			applymsg = userService.selectApplyMsgByUsersequenceAndMsgsequence(Integer.valueOf(usersequence),Integer.valueOf(msgsequence));
			userService.insertFriend(Integer.valueOf(usersequence), applymsg.getSequence(), applymsg.getUserid(), applymsg.getNickname(),remark , applymsg.getSex(), applymsg.getEmail(), applymsg.getPhonenumber(),applymsg.getImg());
			userService.insertFriend(applymsg.getSequence(),u.getSequence(),u.getUserid(),u.getNickname(),applymsg.getRemark(),u.getSex(),u.getEmail(),u.getPhonenumber(),u.getImg());
			userService.findFriendApply(applymsg.getSequence(),u.getSequence(),u.getUserid(),u.getNickname(),u.getEmail(),u.getPhonenumber(),applymsg.getRemark(),"好友已同意您的申请",create_dt,u.getSex(),2,u.getImg());				
			if(Integer.valueOf(usersequence) >= applymsg.getSequence()){
				userService.createChatTableBySequence(applymsg.getSequence(),Integer.valueOf(usersequence));
			}else{
				userService.createChatTableBySequence(Integer.valueOf(usersequence),applymsg.getSequence());
			}
			//ɾ��
			userService.deleteFriendApplyBySequence(Integer.valueOf(usersequence),Integer.valueOf(msgsequence));
			
			result.setDes("添加好友成功");
		}catch(Exception e){
			e.printStackTrace();
			result.setCode("0");
			result.setDes(e.getMessage());
		}
		returnMap.put("result", result);
		return returnMap;
	}
	
	@RequestMapping("/handle")
	public @ResponseBody
	Map<String, Object> handle(String usersequence, String msgsequence){
		Map<String, Object> returnMap = new HashMap<>();
		Result result=new Result();
		result.setCode("1");
		try{
			userService.deleteFriendApplyBySequence(Integer.valueOf(usersequence),Integer.valueOf(msgsequence));
			result.setDes("消息已处理");
		}catch(Exception e){
			e.printStackTrace();
			result.setCode("0");
			result.setDes(e.getMessage());
		}
		returnMap.put("result", result);
		return returnMap;
	}
	
	@RequestMapping("/disagree")
	public @ResponseBody
	Map<String, Object> disagree(String usersequence, String msgsequence){
		Map<String, Object> returnMap = new HashMap<>();
		Result result=new Result();
		result.setCode("1");
		Date date = new Date();       
		Timestamp create_dt = new Timestamp(date.getTime());
		ApplyMsg applymsg =new ApplyMsg();
		try{
			Student u = userService.queryStudentBySequence(Integer.valueOf(usersequence));
			applymsg = userService.selectApplyMsgByUsersequenceAndMsgsequence(Integer.valueOf(usersequence),Integer.valueOf(msgsequence));
			userService.findFriendApply(applymsg.getSequence(),u.getSequence(),u.getUserid(),u.getNickname(),u.getEmail(),u.getPhonenumber(),applymsg.getRemark(),"好友已拒绝您的申请",create_dt,u.getSex(),3,u.getImg());				
			userService.deleteFriendApplyBySequence(Integer.valueOf(usersequence),Integer.valueOf(msgsequence));
		
			result.setDes("对方已拒绝您的请求");
		}catch(Exception e){
			e.printStackTrace();
			result.setCode("0");
			result.setDes(e.getMessage());
		}
		returnMap.put("result", result);
		return returnMap;
	}

}
